﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public partial class RedirectType {

    public string Status {
        get {
            return string.Format("{0} {1}", this.StatusCode, this.StatusDescription);
        }
    }

}

public partial class Host {

    partial void OnHostNameChanged() {
        this._HostName = this._HostName.ToLower();
    }

}

public partial class Entry {

    public static string NormalizeLocalPath(string s) {
        if (string.IsNullOrEmpty(s)) return "/";
        s = s.ToLower();
        while (s.IndexOf("//") > -1) s = s.Replace("//", "/"); // remove all double slashes
        s = s.Trim('/');
        if (!s.StartsWith("/")) s = "/" + s;
        return s;
    }

    public static bool Exists(string hostName, string path) {
        // Validate arguments
        if (hostName == null) throw new ArgumentNullException("hostName");
        if (string.IsNullOrEmpty(hostName)) throw new ArgumentException("Value cannot be null or empty string.", "hostName");

        // Check that entry exists
        using (var dc = new SwitchboardDataContext()) {
            return dc.Entries.Any(x => x.Host.HostName == hostName.ToLower() && x.LocalPath == NormalizeLocalPath(path));
        }
    }

    public static bool Exists(int hostId, string path) {
        using (var dc = new SwitchboardDataContext()) {
            return dc.Entries.Any(x => x.HostId == hostId && x.LocalPath == NormalizeLocalPath(path));
        }
    }

    partial void OnLocalPathChanged() {
        this._LocalPath = NormalizeLocalPath(this._LocalPath);
    }

    public string Location {
        get {
            return string.Format("http://{0}{1}", this.Host.HostName, this.LocalPath);
        }
    }

}